package com.funo.business.ctrler;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.funo.business.entities.Company;
import com.funo.business.service.CompanyService;
import com.funo.common.factory.JsonMsgFactory;
import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.utils.Constant;
import com.funo.core.base.BaseCtrler;
import com.funo.main.entities.User;

@Controller
@RequestMapping("/company")
public class CompanyCtrler extends BaseCtrler{
	@Autowired
	@Qualifier("companyService")
	private CompanyService companyService;
	
	/**
	 * 添加公司
	 * @param company
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public String add(Company company ){
		if (company!=null) {
			try {
				Integer type = companyService.saveCompany(company);
				if (type == Constant.STATUS_SUSSESS) {//成功
					return toJson(JsonMsgFactory.successMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
				return toJson(JsonMsgFactory.failMsg());
			}
		}
		return null;
	}
	
	/**
	 * 修改公司
	 * @param company
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public String edit(Company company){
		System.out.println(company.toString());
		try {
			Integer type = companyService.updateCompany(company);
			if (type == Constant.STATUS_SUSSESS) {//成功
				return toJson(JsonMsgFactory.successMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
		return null;
	}
	
	/**
	 * 获取
	 * @param userId
	 * @param companyId
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/loadCompany",method=RequestMethod.POST)
	@ResponseBody
	public String loadCompany(@RequestParam(value="userId")String userId,@RequestParam(value="companyId")String companyId,HttpSession session){
		Company company = null;
		//通过companyId获取，测试用
		if (companyId!=null || !"".equals(companyId)) { 
			company = companyService.getCompany(companyId);
		}else { //正式用　
			User user = sessionUser(session);
			if(user ==null || user.getId() == null){//丢失session
				return toJson(JsonMsgFactory.fail("请先登录,再进行操作!"));
			}
			company = companyService.getCompanyByUserId(user.getId());
		}
		return toJson(company);
	}
	
	/**
	 * 删除
	 * @param deleteIds
	 * @return
	 */
	@RequestMapping(value="/del",method=RequestMethod.POST)
	@ResponseBody
	public String del(String deleteIds){
		try {

			String[]Ids=deleteIds.split(",");//传递到数据库的参数

			for (int i = 0; i < Ids.length; i++) {
				String id=Ids[i];
				companyService.delCompany(id);
			}
			return toJson(JsonMsgFactory.successMsg());
		} catch (Exception e) {
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	/**
	 * 公司列表
	 * @param condition
	 * @param company
	 * @return
	 */
	@RequestMapping(value="/page")
	@ResponseBody
	public String page(QueryCondition condition ,Company company){
		PageResult<Company> page = companyService.listPage(condition, company);
		return toJson(page);
	}
	
	@Override
	protected String handlerException(Exception e) {
		e.printStackTrace();
		System.err.println("[handlerException:MemberController]异常类型："+e.getClass().getSimpleName());
		System.err.println(e.getMessage());
		return "系统异常";
	}
}
